% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

clear all
if ~isfolder('Results')
    mkdir('.','Results');
end

addpath('../Auxiliary_Files/')
%initialize timeline
big_timeline=(1986:0.25:2017.75)';
[bench_germany,txt,raw]=xlsread('irt_lt_mcby_q.xlsx','Data','BZ17:EW17');

spread_data_matrix=[big_timeline];

figure
subplot(2,1,1)
%% Italy
% Italy Bond 1
[num,txt,raw]=xlsread('Italy.xlsx','Italy','A6:C6007');
spread_data=get_spread_data(raw);
[data_bond1,data_mat_end_of_quarter1, pos]=aggregate_spread_data(spread_data,big_timeline);
dates=data_mat_end_of_quarter1(:,pos.date);
yield1=data_mat_end_of_quarter1(:,pos.yield);
bench1=data_mat_end_of_quarter1(:,pos.bench);
spread1=data_mat_end_of_quarter1(:,pos.spread);
plothandle1=plot(dates,spread1,'b-',dates,yield1,'r-',dates,bench1,'k--');
hold on



% Italy Bond 2
[num,txt,raw]=xlsread('Italy.xlsx','Italy','F25:H2610');
spread_data=get_spread_data(raw);
[data_bond2,data_mat_end_of_quarter2, pos]=aggregate_spread_data(spread_data,big_timeline);
dates=data_mat_end_of_quarter2(:,pos.date);
yield2=data_mat_end_of_quarter2(:,pos.yield);
bench2=data_mat_end_of_quarter2(:,pos.bench);
spread2=data_mat_end_of_quarter2(:,pos.spread);
plothandle2=plot(dates,spread2,'b-',dates,yield2,'r-',dates,bench2,'k--');

spread_data_matrix=[spread_data_matrix nanmean([data_bond1(:,2),data_bond2(:,2)],2)];

[num,txt,raw]=xlsread('irt_lt_mcby_q.xlsx','Data','BZ24:EW24');
spread_data_matrix(big_timeline>=1999,end)=num-bench_germany;
plothandle3=plot(big_timeline(big_timeline>=1999),num-bench_germany,'b-',big_timeline(big_timeline>=1999),num,'r-',big_timeline(big_timeline>=1999),bench_germany,'k--')

axis tight
title('Italy','FontSize',10)
aaa=ylim;
bottom=aaa(1,1);
top=aaa(1,2);

ha = area([min(data_bond1(~isnan(data_bond1(:,2)),1)) max(data_bond1(~isnan(data_bond1(:,2)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.92 0.92 0.92],'EdgeColor','white');
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')
ha1 = area([min(data_bond2(~isnan(data_bond2(:,2)),1)) max(data_bond2(~isnan(data_bond2(:,2)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[1 1 1],'EdgeColor','white');
set(ha1(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha1, 'LineStyle', '-')
ha2 = area([min(big_timeline(big_timeline>=1999)) max(big_timeline(big_timeline>=1999))], [bottom top-bottom; bottom top-bottom],'FaceColor',[1 1 1],'EdgeColor','white');
set(ha2(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha2, 'LineStyle', '-')
xlim([min(min(data_bond1(~isnan(data_bond1(:,2)),1)),min(data_bond2(~isnan(data_bond2(:,2)),1)))  max(big_timeline(big_timeline>=1999))])

ha = area([min(data_bond2(~isnan(data_bond2(:,2)),1)) min(data_bond1(~isnan(data_bond1(:,2)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.92 0.92 0.92],'EdgeColor','white');
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')


% plot(ones(2,1)*[min(data_bond1(~isnan(data_bond1(:,2)),1))],[bottom top],'r--')
% plot(ones(2,1)*[min(data_bond2(~isnan(data_bond2(:,2)),1))],[bottom top],'r--',ones(2,1)*[max(data_bond2(~isnan(data_bond2(:,2)),1))],[bottom top],'r--')
% plot(ones(2,1)*[min(big_timeline(big_timeline>=1999))],[bottom top],'r--',ones(2,1)*[max(big_timeline(big_timeline>=1999))],[bottom top],'r--')

ll=legend(plothandle1,'Spread','Yield','Benchmark')
set(ll,'Box','off')
uistack(plothandle1,'top')
uistack(plothandle2,'top')
uistack(plothandle3,'top')
set(gca,'Layer','top')
text(1990,9.5,'B1','HorizontalAlignment','center')
text(1995,9.5,'B1+B2','HorizontalAlignment','center')
text(1998.5,9.5,'B2','HorizontalAlignment','center')
text(2005,9.5,'Long Term Converg. Rates','HorizontalAlignment','center')

% print -depsc2 Italy_bonds

%initialize timeline
big_timeline=(1986:0.25:2017.75)';

subplot(2,1,2)

%% UK
% UK Bond 1
[num,txt,raw]=xlsread('UK.xlsx','UK','B25:D2478');
spread_data=get_spread_data(raw);
[data_mat1,data_mat_end_of_quarter1, pos]=aggregate_spread_data(spread_data,big_timeline);
dates=data_mat_end_of_quarter1(:,pos.date);
yield1=data_mat_end_of_quarter1(:,pos.yield);
bench1=data_mat_end_of_quarter1(:,pos.bench);
spread1=data_mat_end_of_quarter1(:,pos.spread);
plothandle1=plot(dates,spread1,'b-',dates,yield1,'r-',dates,bench1,'k--');
hold on

% UK Bond 2
[num,txt,raw]=xlsread('UK.xlsx','UK','I25:K1339');
spread_data=get_spread_data(raw);
[data_mat2,data_mat_end_of_quarter2, pos]=aggregate_spread_data(spread_data,big_timeline);
dates=data_mat_end_of_quarter2(:,pos.date);
yield2=data_mat_end_of_quarter2(:,pos.yield);
bench2=data_mat_end_of_quarter2(:,pos.bench);
spread2=data_mat_end_of_quarter2(:,pos.spread);
plothandle2=plot(dates,spread2,'b-',dates,yield2,'r-',dates,bench2,'k--');


CDS_data=load('CDS_data');


plothandle3=plot(CDS_data.timeline,CDS_data.CDS_data_matrix_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))),'b-');
plothandle4=plot(CDS_data.timeline,CDS_data.CDS_data_matrix_TR_Dollar_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))),'b-');

axis tight
title('United Kingdom','FontSize',10)
aaa=ylim;
bottom=aaa(1,1);
top=aaa(1,2);

%first area white
ha = area([min(data_mat_end_of_quarter1(~isnan(data_mat_end_of_quarter1(:,4)),1)) max(data_mat_end_of_quarter1(~isnan(data_mat_end_of_quarter1(:,4)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[1 1 1],'EdgeColor','white');
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')

%intermediate part gray
ha1 = area([max(data_mat_end_of_quarter1(~isnan(data_mat_end_of_quarter1(:,4)),1)) min(data_mat_end_of_quarter2(~isnan(data_mat_end_of_quarter2(:,4)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.92 0.92 0.92],'EdgeColor','white');
set(ha1(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha1, 'LineStyle', '-')


%second area white
ha2 = area([min(data_mat_end_of_quarter2(~isnan(data_mat_end_of_quarter2(:,4)),1)) max(data_mat_end_of_quarter2(~isnan(data_mat_end_of_quarter2(:,4)),1))], [bottom top-bottom; bottom top-bottom],'FaceColor',[1 1 1],'EdgeColor','white');
set(ha2(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha2, 'LineStyle', '-')

%intermediate part gray
ha3 = area([max(data_mat_end_of_quarter2(~isnan(data_mat_end_of_quarter1(:,4)),2)) min(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))))))], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.92 0.92 0.92],'EdgeColor','white');
set(ha3(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha3, 'LineStyle', '-')

%third area white
ha4 = area([min(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:)))))) min(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_TR_Dollar_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))))))], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.92 0.92 0.92],'EdgeColor','white');
set(ha4(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha4, 'LineStyle', '-')

%third area white
ha5 = area([min(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_TR_Dollar_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:)))))) max(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_TR_Dollar_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))))))], [bottom top-bottom; bottom top-bottom],'FaceColor',[1 1 1],'EdgeColor','white');
set(ha5(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha5, 'LineStyle', '-')


xlim([min(min(data_mat_end_of_quarter1(~isnan(data_mat_end_of_quarter1(:,4)),1)),min(data_mat_end_of_quarter2(~isnan(data_mat_end_of_quarter2(:,4)),1)))  max(CDS_data.timeline(~isnan(CDS_data.CDS_data_matrix_TR_Dollar_end_quarter(:,strcmp('United Kingdom',CDS_data.country_header(1,:))))))])

set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')


% plot(ones(2,1)*[min(data_bond1(~isnan(data_bond1(:,2)),1))],[bottom top],'r--')
% plot(ones(2,1)*[min(data_bond2(~isnan(data_bond2(:,2)),1))],[bottom top],'r--',ones(2,1)*[max(data_bond2(~isnan(data_bond2(:,2)),1))],[bottom top],'r--')
% plot(ones(2,1)*[min(big_timeline(big_timeline>=1999))],[bottom top],'r--',ones(2,1)*[max(big_timeline(big_timeline>=1999))],[bottom top],'r--')

% legend(plothandle1,'Spread','Yield','Benchmark')
uistack(plothandle1,'top')
uistack(plothandle2,'top')
uistack(plothandle3,'top')
uistack(plothandle4,'top')
set(gca,'Layer','top')
text(1997,7.5,'B1','HorizontalAlignment','center')
text(2005,7.5,'B2','HorizontalAlignment','center')
text(2008,7.5,'CMA','HorizontalAlignment','center')
text(2008,6.7,'CDS','HorizontalAlignment','center')
text(2014,7.5,'TR','HorizontalAlignment','center')
text(2014,6.7,'CDS','HorizontalAlignment','center')

print('-depsc2','Results/Spread_Construction')

